package foundation;

import com.google.common.collect.Maps;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * <h1></h1>
 *
 * @author zhh 2019-05-30
 */
public class JsonObject {
    public static void main(String[] args) {
        jsonObject();
        System.out.println("----------");
        createJsonObjectByMap();
        System.out.println("----------");
        createJsonObjectByBean();
    }

    private static void jsonObject() {
        JSONObject object = new JSONObject();
        object.put("name", "mike");
        object.put("age", 18);
        object.put("birthday", "2010-01-01");
        object.put("hobby", new String[]{"sing", "dance"});
        System.out.println(object);
    }

    private static void createJsonObjectByMap() {
        HashMap<String, Object> map = Maps.newHashMap();
        map.put("name", "john");
        map.put("age", 18);
        map.put("birthday", "2010-02-01");
        map.put("hobby", new String[]{"sing", "dance"});
        System.out.println(new JSONObject(map));
    }

    private static void createJsonObjectByBean() {
        Student student = new Student();
        student.setName("jean");
        student.setAge(16);
        student.setBirthday("2010-03-01");
        student.setHobby(new String[]{"sing", "dance"});
        System.out.println(new JSONObject(student));
    }
}
